package day041;

//import javax.jws.soap.SOAPBinding;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class 服务端  {
    ObjectOutputStream oos; //准备一个东西的发送管道
    ObjectInputStream ois;  //准备一个东西的接收管道
    Scanner sc = new Scanner(System.in); //获取输入
    public void start() throws IOException {
        ServerSocket ss = new ServerSocket(4444);  //创建服务
        Socket s = ss.accept();  //等待
        OutputStream out = s.getOutputStream(); //获取初级管道
        InputStream in = s.getInputStream();
        oos = new ObjectOutputStream(out);  //给上面的东西管道，赋值
        ois = new ObjectInputStream(in);
        new 接收().start();
        new 发送().start();

    }

//    SuperRobot sr = new SuperRobot();
    class 接收 extends Thread {
        public void run() {
            for (;;) {
                try {
                    User u = (User)ois.readObject();  //接收东西
                    System.out.println(u.intName+"说"+u.isay); //展示东西
//                    sr.speech(100,10,u.intName+"对您说"+ u.isay);
                } catch (IOException | ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    class 发送 extends Thread {
        public void run() {
            for (;;) {
                System.out.println("请您输入："); //输入要发送的内容
                String isay = sc.next();
                User u = new User();  // 将内容存放进东西里面
                u.isay = isay; // 获取用户的输入
                try {
                    oos.writeObject(u); //发送东西
                    oos.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        new 服务端().start();
    }
}
